#include "SpeciesProperties.h"
#include <algorithm>
using namespace slotsdata;

void SpeciesProperties::addFile(QString filename, const geom::PolyObject & object)
{
	m_speciesObjects.insert(std::pair<QString,geom::PolyObject>(filename, object));
}

void SpeciesProperties::setSpeciesFile(int speciesID, QString filename)
{
	m_modelFiles.insert(std::pair<int,QString>(speciesID, filename));
	//checkDanglingObjects(); // FIXME -> this seemed to remove too many objects

}

void SpeciesProperties::checkDanglingObjects()
{
	std::map<int,QString>::iterator it;
	std::map<int,QString>::iterator end = m_modelFiles.end();

	std::vector<QString> files;
	for(it = m_modelFiles.begin(); it!=end; ++it)
		files.push_back(it->second);
	
	std::map<QString,geom::PolyObject>::iterator it2;

	for(it2 = m_speciesObjects.begin(); it2!=m_speciesObjects.end(); ++it2)
		if(std::find(files.begin(),files.end(),it2->first) == files.end())
		{
			m_speciesObjects.erase(it2);
			it2 = m_speciesObjects.begin();
		}
}

QString SpeciesProperties::getFileForSpecies(int speciesID) const
{
	std::map<int,QString>::const_iterator it = m_modelFiles.find(speciesID);

	if(it == m_modelFiles.end())
		return QString("");
	else 
		return it->second;
}
